using System;
using FireStarter.Base;
using FireStarterModeller.BO;
using Habanero.Base.Exceptions;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for UIParameter objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for UIParameter.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestUIParameter
    {
        [SetUp]
        public override void SetupTest()
        {
            //Runs every time that any testmethod is executed
            base.SetupTest();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //------------------------------------------------------------
            // Use this list to ignore generated tests that are failing
            // due to a unique condition in your application.
            // Remember to reimplement the test here.
            //------------------------------------------------------------
            //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
            base.SetupFixture();
        }

        [TearDown]
        public override void TearDownTest()
        {
            //runs every time any testmethod is complete
            base.TearDownTest();
        }

        [Test]
        public void Test_ToString_ShouldRetUIPropType_UIPropName_ParamName()
        {
            //---------------Set up test pack-------------------
            UIParameter uiParameter = new UIParameter();
            var uiProp = TestUtilsUIField.CreateSavedUIField();
            uiParameter.Name = TestUtilsShared.GetRandomString();
            uiParameter.UIProp = uiProp;
            //---------------Assert Precondition----------------
            Assert.IsNotNullOrEmpty(uiProp.PropertyName);
            Assert.IsNotNullOrEmpty(uiParameter.PropertyName);
            Assert.IsNotNullOrEmpty(uiParameter.Name);         
            //---------------Execute Test ----------------------
            var actualToString = uiParameter.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(uiProp.UIPropType + "_" + uiProp.PropertyName +"_" + uiParameter.Name, actualToString);
        }
        [Test]
        public void Test_ToString_WhenGridFilter_ShouldRetUIPropType_UIPropName_ParamName()
        {
            //---------------Set up test pack-------------------
            UIParameter uiParameter = new UIParameter();
            var uiProp = TestUtilsUIGridFilterProp.CreateSavedUIGridFilterProp();
            uiParameter.Name = TestUtilsShared.GetRandomString();
            uiParameter.UIProp = uiProp;
            //---------------Assert Precondition----------------
            Assert.IsNotNullOrEmpty(uiProp.PropertyName);
            Assert.IsNotNullOrEmpty(uiParameter.PropertyName);
            Assert.IsNotNullOrEmpty(uiParameter.Name);         
            //---------------Execute Test ----------------------
            var actualToString = uiParameter.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(uiProp.UIPropType + "_" + uiProp.PropertyName +"_" + uiParameter.Name, actualToString);
        }

        [Test]
        public void Test_PropertyName_WhenUIPropNull_ShouldRetEmptyString()
        {
            //---------------Set up test pack-------------------
            UIParameter uiParameter = new UIParameter();
            //---------------Assert Precondition----------------
            Assert.IsNull(uiParameter.UIProp);
            //---------------Execute Test ----------------------
            var propertyName = uiParameter.PropertyName;
            //---------------Test Result -----------------------
            Assert.IsNullOrEmpty(propertyName);
        }

        [Test]
        public void Test_PropertyName_WhenUIPropSet_ShouldRetUIPropPropName()
        {
            //---------------Set up test pack-------------------
            UIParameter uiParameter = new UIParameter();
            var uiProp = TestUtilsUIField.CreateSavedUIField();
            uiParameter.UIProp = uiProp;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiParameter.UIProp);
            Assert.IsNotNullOrEmpty(uiProp.PropertyName);
            //---------------Execute Test ----------------------
            var propertyName = uiParameter.PropertyName;
            //---------------Test Result -----------------------
            Assert.AreEqual(uiProp.PropertyName, propertyName);
        }
        [Test]
        public void Test_InsertNewUIParameterInfo_Invalid()
        {
            //---------------Set up test pack-------------------
            UIParameter uiParameter = new UIParameter();

            //---------------Assert Precondition----------------
            Assert.IsTrue(uiParameter.Status.IsNew);

            //---------------Execute Test ----------------------
            try
            {
                uiParameter.Save();
                Assert.Fail("expected Err");
            }
                //---------------Test Result -----------------------
            catch (BusObjectInAnInvalidStateException ex)
            {
                StringAssert.Contains("Name' is a compulsory field and has no value", ex.Message);
                StringAssert.Contains("Value' is a compulsory field and has no value", ex.Message);
            }
            Assert.IsTrue(uiParameter.Status.IsNew);
        }

        [Test]
        public void Test_InsertNewUIParameterInfo()
        {
            //---------------Set up test pack-------------------
            UIParameter uiParameter = TestUtilsShared.CreateUnsavedValidUIParameterInfo();

            //---------------Assert Precondition----------------
            Assert.IsTrue(uiParameter.Status.IsNew);

            //---------------Execute Test ----------------------
            uiParameter.Save();

            //---------------Test Result -----------------------
            Assert.IsFalse(uiParameter.Status.IsNew);
        }

        [Test]
        public void Test_DeleteUIParameterInfo()
        {
            //---------------Set up test pack-------------------
            UIParameter uiParameter = TestUtilsShared.CreateSavedUIParameterInfo();

            //---------------Assert Precondition----------------
            Assert.IsFalse(uiParameter.Status.IsNew);
            string message;
            Assert.IsTrue(uiParameter.IsDeletable(out message));

            //---------------Execute Test ----------------------
            uiParameter.MarkForDelete();
            uiParameter.Save();

            //---------------Test Result -----------------------  
            Assert.IsTrue(uiParameter.Status.IsNew);
            Assert.IsTrue(uiParameter.Status.IsDeleted);
        }

        [Test]
        public void Test_UpdateUIParameterInfo()
        {
            //---------------Set up test pack-------------------
            UIParameter uiParameter = TestUtilsShared.CreateSavedUIParameterInfo();

            //---------------Assert Precondition----------------
            Assert.IsFalse(uiParameter.Status.IsNew);
            string message;
            Assert.IsTrue(uiParameter.IsEditable(out message));

            //---------------Execute Test ----------------------
            TestUtilsShared.UpdateUIParameterInfoWithValidData(uiParameter);

            //---------------Test Result -----------------------
            Assert.IsFalse(uiParameter.Status.IsNew);
        }
    [Test]
        public void Test_GetParent()
        {
            //---------------Set up test pack-------------------
            UIGridColumnInfo gridColumnInfo = new UIGridColumnInfo();
            UIParameter uiParameter = new UIParameter();
            gridColumnInfo.UIParameters.Add(uiParameter);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IModelElement parent = ((IModelElement) uiParameter).Parent;
            //---------------Test Result -----------------------
            Assert.AreSame(gridColumnInfo, parent);
        }

        [Test]
        public void Test_SetParentExceptionThrown()
        {
            //---------------Set up test pack-------------------
            IModelElement prop = new UIParameter();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            Exception exceptionThrown = null;
            try
            {
                prop.Parent = new UIGridColumnInfo();
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            //---------------Test Result -----------------------
            Assert.IsNotNull(exceptionThrown);
            Assert.IsInstanceOf(typeof (HabaneroDeveloperException), exceptionThrown);
            StringAssert.Contains
                ("Set the parent of the UIParameter by setting the related UIGridColumnInfo object",
                 exceptionThrown.Message);
        }
    }
    
}